import storageSession from "redux-persist/lib/storage/session";
import {persistReducer} from "redux-persist";

import {CHECK_USER, GET_USER_INFORMATION, LANGUAGE,COEFFICENT_SUPER} from "./constants";

const persistConfig = {
  key: "user",
  storage: storageSession,
  whitelist: ["isUser", "info", "isCN"] //  白名单:isUser会存入缓存，其他不会存
};

const defaultState = {
  isUser: false,
  info: {},
  isCN: false,
  isSuper:false
};

function reducer(state = defaultState, action) {
  switch (action.type) {
    case CHECK_USER:
      return {...state, isUser: action.isCheck};
    case GET_USER_INFORMATION:
      return {...state, info: action.getInfo};
    case LANGUAGE:
      return {...state, isCN: action.isCN};
      case COEFFICENT_SUPER:
      return {...state, isSuper: action.isSuper};
    default:
      return state;
  }
}

//  将persistConfig中的whitelist存储到缓存中(刷新浏览器redux中的此值不会重置),不在白名单里的则正常存储(刷新浏览器redux数据重置)
export default persistReducer(persistConfig, reducer);